<?php
/**
 * Deligator for OutPatient module
 *
 */
$loader->requireOnce('includes/RadConnector/IRadConnector.class.php');
$loader->requireOnce('ordo/TicketItem.class.php');
$loader->requireOnce('includes/PatientQueue/AbsQueue.class.php');
class OutPatientRadConnector implements  IRadConnector {
	private $logger;
	function __construct(){
		$this->logger = LoggerManager::getLogger('includes.RadConnector.OutPatientRadConnector');
	}
    public function doAfterCreateAssignment($data) {
    	$this->logger->info("Start function doAfterCreateAssignment {ticketId=" . $data['ticketId'] . ", radTicketId=" . $data['radTicketId'] . "}");

    	// Create ticket_item record
        $objTicketItem    =   ORDataObject::factory('TicketItem');
        // Item type
        $itemType       =   '';
        $enumRadType    =   $data['radType'];
        if ($enumRadType == Utils::getEnumKey(EnumerationConstant::PARACLINIC_TYPE, EnumerationConstant::PARACLINIC_ENDO)) {
            $itemType   =   TicketItem::ENDO_TYPE;
        }
        else if ($enumRadType == Utils::getEnumKey(EnumerationConstant::PARACLINIC_TYPE, EnumerationConstant::PARACLINIC_XRAY)) {
            $itemType   =   TicketItem::XRAY_TYPE;
        }
        else if ($enumRadType == Utils::getEnumKey(EnumerationConstant::PARACLINIC_TYPE, EnumerationConstant::PARACLINIC_ULTRA)) {
            $itemType   =   TicketItem::ULTRA_TYPE;
        }

        $objTicketItem->addRadAssignmentItem($data['ticketId'], $data['radTicketId'], $itemType,
            $data['roomId'], $data['originalPrice'], $data['insurancePrice'], $data['patientPrice']);
        $objTicketItem->persist();

        $this->logger->info("Finish function doAfterCreateAssignment {ticketId=" . $data['ticketId'] . ", radTicketId=" . $data['radTicketId'] . "}");
    }

    public function doBeforeCreateAssignment($data){

    }

    public function doAfterFinishAssignment($itemId, $itemType, $data){
        // Update prices of the ticket_item
        $this->logger->info("Start function doAfterFinishAssignment {itemId=$itemId, itemType=$itemType}");
        // Update prices of the ticket item
        $objTicketItem  =   ORDataObject::factory('TicketItem');
        $objTicketItem->updatePrices($itemId, $itemType, $data);

        // Change status of the ticket item to closed
        $ticketItemId   =   $objTicketItem->getTicketItemId($itemId, $itemType);
        $objTicketItem->changeStatus($ticketItemId, TicketItem::CLOSED);
        $objTicketItem->close($ticketItemId);
        $this->logger->info("Finish function doAfterFinishAssignment {itemId=$itemId, itemType=$itemType}");
    }

    public function doAfterDeleteAssignment($itemId, $enumItemType) {
        $this->logger->info("Start function doAfterDeletingAssignment {itemId=$itemId, enumItemType=$enumItemType}");
        $objTicketItem  =   ORDataObject::factory('TicketItem');
        $ticketItemId   =   $objTicketItem->getTicketItemId($itemId, $enumItemType);
        $objTicketItem->delete($ticketItemId);
        $this->logger->info("Finish function doAfterDeletingAssignment {itemId=$itemId, enumItemType=$enumItemType}");
    }

    public function doAfterCancelAssignment($radTicketId, $enumRadType) {
        $this->logger->info("Start function doAfterCancelAssignment {radTicketId=$radTicketId, enumRadType=$enumRadType}");
        $objTicketItem  =   ORDataObject::factory('TicketItem');
    	$ticketItemId   =   $objTicketItem->getTicketItemId($radTicketId, $enumRadType);
        if ($ticketItemId != 0) {
        	$this->logger->debug("Move ticket item to queue {ticketItemId=$ticketItemId, destQueue=}". AbsQueue::ENCOUNTER);
            //AbsQueue::moveTicketItemToQueue($ticketItemId, AbsQueue::ENCOUNTER);
            $objTicketItem->changeStatus($ticketItemId, TicketItem::OPEN);
        }
        $this->logger->info("Finish function doAfterCancelAssignment {radTicketId=$radTicketId, enumRadType=$enumRadType}");
    }

    public function doAfterStartAssignment($radTicketId, $enumRadType) {
        $this->logger->info("Start function doAfterStartAssignment {radTicketId=$radTicketId, enumRadType=$enumRadType}");
        $ticketItemId   =   ORDataObject::factory('TicketItem')->getTicketItemId($radTicketId, $enumRadType);
        if ($ticketItemId != 0) {
            //AbsQueue::moveTicketItemToQueue($ticketItemId, AbsQueue::IN_PROGRESS);
            $objTicketItem->changeStatus($ticketItemId, TicketItem::IN_PROGRESS);
        }
        $this->logger->info("Finish function doAfterStartAssignment {radTicketId=$radTicketId, enumRadType=$enumRadType}");
    }
}
?>